package architect.algorithn;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 协同推荐SlopeOne
 *
 * @author guanxin
 * @date 2018/5/8 11:25
 */

public class CollaborativeRecommendation {

    public static void main(String[] args) {
        SlopeOne slopeOne = new SlopeOne();

        Map<Integer, List<Product>> userRating = new HashMap<>();

        // 第一位用户
        List<Product> list = Stream.of(
                new Product(1, "洗衣机", 5f),
                new Product(2, "电冰箱", 10f),
                new Product(3, "彩电", 10f),
                new Product(4, "空调", 5f)
        ).collect(Collectors.toList());

        //userRating.put(1000, list);
        //slopeOne.addUserRatings(userRating);

        userRating.clear();
        userRating.put(1000, list);
        slopeOne.addUserRatings(userRating);

        // 第二位用户
        list = Stream.of(
                new Product(1, "洗衣机", 4f),
                new Product(2, "电冰箱", 5f),
                new Product(3, "彩电", 4f),
                new Product(4, "空调", 10f)
        ).collect(Collectors.toList());

        userRating.clear();
        userRating.put(2000, list);
        slopeOne.addUserRatings(userRating);

        // 第三位用户
        list = Stream.of(
                new Product(1, "洗衣机", 4f),
                new Product(2, "电冰箱", 10f),
                new Product(4, "空调", 5f)
        ).collect(Collectors.toList());

        userRating.clear();
        userRating.put(3000, list);
        slopeOne.addUserRatings(userRating);

        //那么我们预测userID=3000这个人对 “彩电” 的打分会是多少？
        int userId = userRating.keySet().iterator().next();
        List<Product> result = userRating.get(userId);

        Map<Integer, Float> predictions = slopeOne.predict(result);

        predictions.forEach((k, v) -> {
            System.out.printf("ProductID: %s Rating: %s", k, v);
        });
    }
}
